package com.healthyeating.manage.controller;

import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Comment;
import com.healthyeating.manage.domain.RecipeComment;
import com.healthyeating.manage.domain.vo.CommentVo;
import com.healthyeating.manage.domain.vo.RecipeCommentVo;
import com.healthyeating.manage.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 评论Controller
 *
 * @author 段奇乐
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/manage/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('manage:share:list')")
    @GetMapping("/{shareId}")
    public AjaxResult list(@PathVariable("shareId") Long shareId) {
        List<CommentVo> commentVoList = commentService.selectCommentListByShareId(shareId);
        return success(commentVoList);
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('manage:share:add')")
    @Log(title = "新增评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment, HttpServletRequest request) {
        return toAjax(commentService.insertComment(comment, request));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('manage:share:edit')")
    @Log(title = "修改评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommentVo commentVo) {
        return toAjax(commentService.updateComment(commentVo));
    }

    /**
     * 查询食谱评论列表
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:list')")
    @GetMapping("/recipe/{recipeId}")
    public AjaxResult listRecipeComment(@PathVariable("recipeId") Long recipeId) {
        List<RecipeCommentVo> commentVoList = commentService.selectCommentListByRecipeId(recipeId);
        return success(commentVoList);
    }

    /**
     * 新增食谱评论
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:add')")
    @Log(title = "新增评论", businessType = BusinessType.INSERT)
    @PostMapping("/recipe/addComment")
    public AjaxResult add(@RequestBody RecipeComment comment, HttpServletRequest request) {
        return toAjax(commentService.insertRecipeComment(comment, request));
    }
}
